<template>
	<view>
		<u-cell  :border='false' title=" "></u-cell>
		<u-row>
			<u-col span="8" offset="2" ><u--input v-model="mobile" shape="circle"  type="number" placeholder="请输入手机号"></u--input></u-col>
		</u-row>
		<u-cell  :border='false' title=" "></u-cell>
		<u-row>
			<u-col span="8" offset="2"  ><u--input shape="circle" :password = 'true' v-model="password" placeholder="新的密码"></u--input></u-col>
		</u-row>
		<u-cell  :border='false' title=" "></u-cell>
		<u-row>
			<u-col span="8" offset="2" class="u-m-t-20">
				<!-- 注意：由于兼容性差异，如果需要使用前后插槽，nvue下需使用u--input，非nvue下需使用u-input -->
							<!-- #ifndef APP-NVUE -->
							<u-input placeholder="验证码" type="number" shape="circle"  v-model="code">
							<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							<u--input placeholder="验证码" type="number" shape="circle"  v-model="code">
											>
							<!-- #endif -->
								<template slot="suffix"> 
								<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
									<u-button
										@tap="getCode" :text="tips"  
										type="success"
										size="mini"
									></u-button>
								</template>
							<!-- #ifndef APP-NVUE -->
							</u-input>
							<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							</u--input>
							<!-- #endif -->
		 
			</u-col>
		</u-row>
		<u-cell  :border='false' title=" "></u-cell>
		<u-cell  :border='false' title=" "></u-cell>
		<u-row>
			<u-col span="8" offset="2" ><u-button type="primary" shape="circle"  @click="reset" text="重置"></u-button></u-col>
		</u-row>
	</view>
</template>

<script>
var that
var UserInfo = require('../../../../service/UserInfo.js'),mUserInfo
var ERCode = require('../../../../service/ERCode.js'),mERCode
export default {
	data() {
		return {
			tips:'',
			mobile:'',
			password:'',
			code:'',
			id:''
		};
	},
	onLoad() {
		that = this
		mUserInfo = new UserInfo(this.mNetTool)
		mERCode = new ERCode(this.mNetTool)
	},
	methods: {
		reset(){
			uni.showLoading({
				title:'请稍等'
			})
			if(!uni.$u.test.mobile(that.mobile)){
				uni.$u.toast('请输入正确的手机号');
				return
			}
			if(that.password.length <= 6){
					uni.$u.toast('新密码长度必须大于6位 ');
					return
			}
			// if(!uni.$u.test.isEmpty(that.password)){ 
			// 	console.log(uni.$u.test.isEmpty(that.password))
			// 	uni.$u.toast('请输入新密码');
			// 	return
			// }
			if(that.code && that.id){
				mERCode.checkCode({
					mobile:that.mobile,
					code:that.code
				},res => {
					if(res.success){
						mUserInfo.update({
							id:that.id,
							password:that.password
						},res => {
							if(res.success){
								uni.hideLoading()
								uni.$u.toast('重置成功！请登录')
								uni.navigateBack({
									
								})
							}else{
								uni.$u.toast('请求失败')
							}
						})
					}
				})
			}
		},
		codeChange(text) {
			this.tips = text;
		},
		getCode() {
			if(!uni.$u.test.mobile(that.mobile)){
				uni.$u.toast('请输入正确的手机号');
				return
			}
			if(!uni.$u.test.isEmpty(that.password)){
				uni.$u.toast('请输入新密码');
				return
			}
			if (this.$refs.uCode.canGetCode) {
				uni.showLoading({
					title: '正在获取验证码'
				});
			 
				mUserInfo.list({
					mobile:that.mobile
				},res => {
					if(res.records === 1){
						that.id = res.rows[0].id
						mERCode.getCode({
							mobile:that.mobile
						},R => {
							if(R.success){
								this.$refs.uCode.start();
								uni.$u.toast('验证码已发送');
								uni.hideLoading()
							}
						})
					}else{
							uni.$u.toast('改号码未注册');
							uni.hideLoading()
					}
				})
				
				
				// setTimeout(() => {
				// 	uni.hideLoading();
				// 	// 这里此提示会被this.start()方法中的提示覆盖
				// 	uni.$u.toast('验证码已发送');
				// 	// 通知验证码组件内部开始倒计时
				// 	this.$refs.uCode.start();
				// }, 2000);
			} else {
				uni.$u.toast('倒计时结束后再发送');
			}
		},
		change(e) {
			console.log('change', e);
		}
	}
};
</script>

<style lang="scss"></style>
